package eteam.aps.service.impl;

import eteam.aps.client.CommonClient;
import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.WorkingProcedureMapper;
import eteam.aps.model.dto.WorkingProcedureQueryDto;
import eteam.aps.model.entity.WorkingProcedure;
import eteam.aps.model.vo.working_procedure.WorkingProcedureShowVo;
import eteam.aps.model.vo.working_procedure.WorkingProcedureUpdateVo;
import eteam.aps.model.vo.working_procedure.WorkingProcedureVo;
import eteam.aps.service.WorkingProcedureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工序信息 业务层 实现类;
 *
 * @author LiuLei
 */
@Service
public class WorkingProcedureServiceImpl implements WorkingProcedureService {

    @Autowired
    private WorkingProcedureMapper workingProcedureMapper;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Autowired
    private CommonClient commonClient;

    @Override
    public JsonPage<WorkingProcedureShowVo> selWPJsp(WorkingProcedureQueryDto wpq) throws Exception {
        Map<String, Object> map = new HashMap<>(4);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("offSize", wpq.offSize());
        map.put("size", wpq.getSize());
        if (Helper.isNotEmpty(wpq.getfName())) map.put("fName", Helper.likeSql(wpq.getfName()));
        int count = workingProcedureMapper.selCountByWpq(map);
        if (count == 0) return JsonPage.builder(wpq.getPage(), wpq.getSize(), count, Collections.emptyList());
        map.put("sort", "FCode");
        List<WorkingProcedureShowVo> list = workingProcedureMapper.selWPJsp(map);
        Map<String, String> usMap = commonClient.selectUserName();
        list.forEach(data -> {
            data.setfCreateUserId(usMap.get(data.getfCreateUserId()));
            data.setfLastModifyUserId(usMap.get(data.getfLastModifyUserId()));
        });
        return JsonPage.builder(wpq.getPage(), wpq.getSize(), count, list);
    }

    @Override
    public JsonPage<WorkingProcedureVo> selWPSimJsp(WorkingProcedureQueryDto wpq) {
        Map<String, Object> map = new HashMap<>(5);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("offSize", wpq.offSize());
        map.put("size", wpq.getSize());
        if (Helper.isNotEmpty(wpq.getfName())) map.put("fName", Helper.likeSql(wpq.getfName()));
        int count = workingProcedureMapper.selCountByWpq(map);
        if (count == 0) return JsonPage.builder(wpq.getPage(), wpq.getSize(), count, Collections.emptyList());
        map.put("sort", "FCode");
        return JsonPage.builder(wpq.getPage(), wpq.getSize(), count, workingProcedureMapper.selWPSimJsp(map));
    }

    @Override
    public int selByProNameAndProScn(String fName, String fCode) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("fName", fName);
        map.put("fCode", fCode);
        return workingProcedureMapper.selByProNameAndProScn(map);
    }

    @Override
    @Transactional
    public int saveWP(WorkingProcedureUpdateVo wpu) throws Exception {
        WorkingProcedure workingProcedure = new WorkingProcedure();
        BeanUtils.copyProperties(wpu, workingProcedure);
        Helper.setDefaultAddValue(workingProcedure, loginUserRedisService.getUser().getFuserid());
        return workingProcedureMapper.saveWP(workingProcedure);
    }

    @Override
    @Transactional
    public int delWP(String fId) throws Exception {
        Map<String, Object> map = new HashMap<>(4);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("fId", fId);
        map.put("fLastModifyTime", LocalDateTime.now());
        map.put("fLastModifyUserId", loginUserRedisService.getUser().getFuserid());
        return workingProcedureMapper.delWP(map);
    }

    @Override
    @Transactional
    public int updateWP(WorkingProcedureUpdateVo wpu) throws Exception {
        Helper.setEmptyWithNull(wpu);
        WorkingProcedure workshopSection = new WorkingProcedure();
        BeanUtils.copyProperties(wpu, workshopSection);
        workshopSection.setfLastModifyTime(LocalDateTime.now());
        workshopSection.setfLastModifyUserId(loginUserRedisService.getUser().getFuserid());
        return workingProcedureMapper.updateWP(workshopSection);
    }
}